package com.hongdee.crm.Template.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.Template.entity.TCrmTemplateRef;
import com.hongdee.crm.Template.service.ITCrmTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *  名  称:
 * 类  型: class
 * 描  述: 模块对应管理操作接口
 * 路  径:com.hongdee.crm.template.controller
 * 作  者:于扬
 * 日  期:2017/5/4 15:41
 */
@RestController
@RequestMapping("/crm/template")
public class TCRMTemplateController {
    @Autowired
    private ITCrmTemplateService itCrmTemplateService;

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmTemplateRef ref) throws Exception {
        TCrmTemplateRef group = itCrmTemplateService.saveOrUpdate(ref);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /*
    * 查询
    * */


    @RequestMapping("/info")
    @ResponseBody
        public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
            ResultEntity res = new ResultEntity();
            Map map = itCrmTemplateService.info(id);
            res.setData(map);
            res.setSuccess(true);
            res.setMsg("查询详情成功");
            return res;
        }

    /**
     * 停用
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmTemplateService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 停用
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmTemplateService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }

    /*
    * 分页
    * */
    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "templateNum", defaultValue = "") String templateNum
    ) throws Exception {
        Map map = this.itCrmTemplateService.newPage(start, limit, templateNum);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
}
